﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;

namespace BookStore.Solution.Presentation
{
    public partial class ucPromotion : UserControl
    {
        PromotionManager promotionManager = new PromotionManager();
        public ucPromotion()
        {
            InitializeComponent();
        }

        private bool SetRole()
        {
            bool t = false;
            List<string> roles = AccountManager.GetRoleName();
            btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = false;
            btnSearchbyID.Enabled = btnSearchbyName.Enabled = false;
            if (roles.Contains("Thêm, xóa, cập nhật khuyến mãi"))
            {
                btnAdd.Enabled = btnDelete.Enabled = btnUpdate.Enabled = true;
                t = true;
            }
            if (roles.Contains("Tìm kiếm khuyến mãi"))
            {
                btnSearchbyID.Enabled = btnSearchbyName.Enabled = true;
                t = true;
            }
            return t;
        }
        private void ucPromotion_Load(object sender, EventArgs e)
        {
            if (SetRole())
            LoadData();
        }

        private void LoadData()
        {
            grvListofPromotion.AutoGenerateColumns = false;
            grvListofPromotion.DataSource = promotionManager.GetAll();
            for (int i = 0; i < grvListofPromotion.Columns.Count; i++)
            {
                grvListofPromotion.Columns[i].DataPropertyName = grvListofPromotion.Columns[i].Name;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmPromotion frm = new frmPromotion();
            frm.ShowDialog();
            if (!frm.Visible)
            {
                LoadData();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string promotionID = "";
            if (grvListofPromotion.SelectedRows.Count > 0)
            {
                promotionID = grvListofPromotion.CurrentRow.Cells["PromotionID"].Value.ToString();
                frmPromotion frm = new frmPromotion(promotionID,false);
                frm.ShowDialog();
                if (!frm.Visible)
                {
                    LoadData();
                }
            }
        }

        private void btnViewDetail_Click(object sender, EventArgs e)
        {
            string promotionID = "";
            if (grvListofPromotion.SelectedRows.Count > 0)
            {
                promotionID = grvListofPromotion.CurrentRow.Cells["PromotionID"].Value.ToString();
                frmPromotion frm = new frmPromotion(promotionID, true);
                frm.ShowDialog();
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            string promotionID = "",result ="";
            if (grvListofPromotion.SelectedRows.Count > 0)
            {
                DialogResult dlr = MessageBox.Show("Bạn có chắc chắn xóa các khuyến mãi đã chọn không?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlr == DialogResult.Yes)
                {
                promotionID = grvListofPromotion.CurrentRow.Cells["PromotionID"].Value.ToString();
                promotionManager = new PromotionManager();
                result = promotionManager.Delete(promotionID);
                if (result != "")
                {
                        DialogResult dlrg = MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (dlrg == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
            }
            }
            else
            {
                MessageBox.Show("Chọn ít nhất 1 khuyến mãi để xóa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSearchbyID_Click(object sender, EventArgs e)
        {
            FrmSearchbyPromotionID frm = new FrmSearchbyPromotionID();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                grvListofPromotion.AutoGenerateColumns = false;
                grvListofPromotion.DataSource = frm.result;
                for (int i = 0; i < grvListofPromotion.Columns.Count; i++)
                {
                    grvListofPromotion.Columns[i].DataPropertyName = grvListofPromotion.Columns[i].Name;
                }
            }
        }

        private void btnSearchbyName_Click(object sender, EventArgs e)
        {
            FrmSearchbyPromotionName frm = new FrmSearchbyPromotionName();
            frm.ShowDialog();
            if (!frm.Visible && frm.result !=null)
            {
                grvListofPromotion.AutoGenerateColumns = false;
                grvListofPromotion.DataSource = frm.result;
                for (int i = 0; i < grvListofPromotion.Columns.Count; i++)
                {
                    grvListofPromotion.Columns[i].DataPropertyName = grvListofPromotion.Columns[i].Name;
                }
            }
        }

        private void grvListofPromotion_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnViewDetail.PerformClick();
        }

        private void tsmiRefesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
